var express = require('express');
var debug = require('debug')('express');
var app = express();
var config = require('./config/config.js')();
// var RedisStore = require('connect-redis')(session);


// start it
app.listen(3000, function () {
    console.log('server listen at 3000...');
});


// log
app.use(function (req, res, next) {
    debug(req.method + "  " + req.url);
    next();
});


// body-parser ok, but i'd like to do this in router not in top level
// see router->test, post /abc
// var bodyParser = require('body-parser');
// app.use(bodyParser.urlencoded());
// app.use(bodyParser.json());


var session = require('express-session');
var uuid = require('node-uuid');
// session default value is { path: '/', httpOnly: true, secure: false, maxAge: null }
app.use(session({
    // redis
    // store: new RedisStore({
    // 	port: config.redis.port,
    // 	host: config.redis.host,
    // 	prefix: 'session:'
    // }),
    genid: function (req) {
        return uuid.v4();
    },
    secret: 'keyboard cat',
    // secret: 'cookie_secret',
    resave: true,
    saveUninitialized: true,
    cookie: {
        maxAge: 3600 * 1000 // one hour
    }
}));
// // test session
// app.use(function(req, res, next) {
//   if (!req.session.visitAt) {
//     req.session.visitAt = new Date();
//   }
//   next();
// })


// public static
app.use(express.static(__dirname + '/public'));
app.use("/blueInk", express.static(__dirname + '/public/blueInk'));


// enable cross domain
app.use(function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", "*");
    res.header("Access-Control-Allow-Headers",
        "Origin, X-Requested-With, Content-Type, Accept");
    if (req.method === 'OPTION') {
        res.send("Access-Control-Allow-Origin");
    } else {
        next();
    }
});


// ------------------router----------------------
// test
app.use('/test', require('./router/test.js'));

//
// app.use('/', require('./router/home.js'));
// app.use('/', require('./router/index.js'));

//
app.use('/admin', require('./router/admin/index.js'));

app.use('/api/v1/admin', require('./router/api/v1/admin.js'));
app.use('/api/v1/share', require('./router/api/v1/share.js'));
app.use('/api/v1/upload', require('./router/api/v1/upload.js'));

// share
app.use('/api/share', require('./router/share.js'));

// upload file
app.use('/api/upload', require('./router/upload.js'));

// auth
// app.use('/auth', require('./router/auth.js'));

// appShare
// app.use('/api/appShare/index', require('./router/appShare/index.js'));
// app.use('/api/appShare/share', require('./router/appShare/share.js'));
// app.use('/api/appShare/admin', require('./router/appShare/admin.js'));


// ------------------router----------------------


// 404
app.use(function (req, res, next) {
    var err = new Error("request no found at: " + req.url);
    err.status = 404;
    next(err);
});

// error handler
app.use(function (err, req, res, next) {
    debug(err.message);
    res.status(err.status || 500).end(err.message || JSON.stringify(err));
});